<?php

function generarAleatorio(){
    $str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
    for($i=0;$i<6;$i++) {
        $cad .= substr($str,rand(0,62),1);
    }
    return $cad;
}

function cargar_wishlist($id){
    
        include_once 'bd_conn_class/conexionMysql.php';
        
        $bd = new conexionMysql();
        //." concat(ph_img_route,'/',ph_img_name) as imagen, "
        $sql = " SELECT "
                ." concat(ph_img_mini_route,'/',ph_img_name) as imagen, "
                ." concat(ph_img_route,'/',ph_img_name) as imagen_full, "
                ." ph_img_name as name, "
                ." ph_desc as descripcion, "
                ." p_id, "
                ." p_l_count, "
                ." p_c_count, "
                ." p_s_count "
              ." FROM wishlist, post, photo "
              ." WHERE wl_u_id = '".$id."' "
                ." AND wl_u_id = p_u_id "
                ." AND wl_u_id = ph_u_id "
                ." AND wl_status = '1' "
                ." AND p_status = '1' "
                ." AND ph_status = '1' "
                ." AND p_wl_id = wl_id "
                ." AND p_ph_id = ph_id "
                ." AND ph_img_mini_route <> '' "
              ." ORDER BY wl_date desc ";

        $bd->ConectarBD();
        $rs = $bd->ObtenerDataEnRecurso($sql);
        
        while ($fila = mysql_fetch_array($rs)){
            $imagenArr[$fila["p_id"]]= array("img"=>$fila["imagen"],
                                "desc"=>$fila["descripcion"],
                                "imgFull"=>$fila["imagen_full"],
                                "imgName"=>$fila["name"],
                                "idPost"=>$fila["p_id"],
                                "likes"=>$fila["p_l_count"],
                                "comments"=>$fila["p_c_count"],
                                "shares"=>$fila["p_s_count"]);
        }
        
        $bd->DesconectarBD();
        
        return $imagenArr;    
    
}

function cargar_comentarios($id, $limit=false){
    
        include_once 'bd_conn_class/conexionMysql.php';
        
        $bd = new conexionMysql();

        $sql = " SELECT pc_id, pc_p_id, pc_desc, "
              ."    pc_date, concat(user_fname, ' ', user_lname) as user_name, "
              ."    user_profile_pic, user_id"
              ." FROM post_comment, user "
              ." WHERE pc_p_id = '".$id."' "
              ."    AND pc_status = 1 "
              ."    AND pc_u_id = user_id "
              ." ORDER BY pc_date desc";

        if($limit>0){
            //$sql .= " LIMIT 0, $limit ";
        }
        
        $bd->ConectarBD();
        $rs = $bd->ObtenerDataEnRecurso($sql);
        
        while ($fila = mysql_fetch_array($rs)){
            $comentarioImagenArr[$fila["pc_id"]]= array("id"=>$fila["pc_id"],
                                                        "fecha"=>$fila["pc_date"],
                                                        "desc"=>$fila["pc_desc"],
                                                        "upic"=>$fila["user_profile_pic"],
                                                        "uname"=>$fila["user_name"],
                                                        "uid"=>$fila["user_id"]);
        }
        
        $bd->DesconectarBD();
        
        return $comentarioImagenArr;
}

function guardar_comentario($action, $comentario, $post_id){
        include_once 'bd_conn_class/conexionMysql.php';
        
        $bd = new conexionMysql();

        $sql = " INSERT INTO post_comment "
              ."     (pc_id, pc_p_id, pc_desc, pc_date, pc_u_id)  "
              ." VALUES "
              ."     (NULL, '".$post_id."', '".$comentario."', NOW(), '".$_SESSION["g2uId"]."') ";  

        $bd->ConectarBD();
        $bd->EjecutarQuery($sql);
        $bd->DesconectarBD();
}

function actualizar_contador_comentario($post_id){
        include_once 'bd_conn_class/conexionMysql.php';
        
        $bd = new conexionMysql();

        $sql = " UPDATE post  "
              ." SET p_c_count =   "
              ."     (SELECT COUNT(*) "
              ."      FROM post_comment "
              ."      WHERE pc_p_id = '".$post_id."' "
              ."          AND pc_status = 1) "
              ." WHERE p_id = '".$post_id."' "
              ."     AND p_status = 1 ";

        $bd->ConectarBD();
        $bd->EjecutarQuery($sql);
        $bd->DesconectarBD();
}

function guardar_like($post_id){
        include_once 'bd_conn_class/conexionMysql.php';
        
        $bd = new conexionMysql();

        $sql = " SELECT 1 as rs"
              ." FROM post_like "
              ." WHERE pl_p_id = '".$post_id."' "
              ."     AND pl_u_id = '".$_SESSION["g2uId"]."' "
              ."     AND pl_status = 1";  

        $bd->ConectarBD();
        $rs = $bd->ObtenerDataEnRecurso($sql);
        $existe = 0;
        while ($fila = mysql_fetch_array($rs)){
            $existe = $fila["rs"];
        }
        $bd->DesconectarBD();
        
        if(!$existe){
            $sql = " INSERT INTO post_like "
                  ."     (pl_id, pl_p_id, pl_u_id, pl_date)  "
                  ." VALUES "
                  ."     (NULL, '".$post_id."',  '".$_SESSION["g2uId"]."', NOW() ) ";  

            $bd->ConectarBD();
            $bd->EjecutarQuery($sql);
            $bd->DesconectarBD();
        } else { 
            $sql = " UPDATE post_like "
                  ." SET pl_status = 0 "
                  ." WHERE pl_p_id = '".$post_id."' "
                  ."     AND pl_u_id = '".$_SESSION["g2uId"]."' ";  

            $bd->ConectarBD();
            $bd->EjecutarQuery($sql);
            $bd->DesconectarBD();
        }
}

function actualizar_contador_like($post_id){
        include_once 'bd_conn_class/conexionMysql.php';
        
        $bd = new conexionMysql();

        $sql = " UPDATE post  "
              ." SET p_l_count =   "
              ."     (SELECT COUNT(*) "
              ."      FROM post_like "
              ."      WHERE pl_p_id = '".$post_id."' "
              ."          AND pl_status = 1) "
              ." WHERE p_id = '".$post_id."' "
              ."     AND p_status = 1 ";

        $bd->ConectarBD();
        $bd->EjecutarQuery($sql);
        $bd->DesconectarBD();
}

function cargar_likes($id){
    
        include_once 'bd_conn_class/conexionMysql.php';
        
        $bd = new conexionMysql();

        $sql = " SELECT pl_id, pl_p_id, pl_date, "
              ."    user_id, concat(user_fname, ' ', user_lname) as user_name "
              ." FROM post_like, user "
              ." WHERE pl_p_id = '".$id."' "
              ."    AND pl_status = 1 "
              ."    AND pl_u_id = user_id ";

        $bd->ConectarBD();
        $rs = $bd->ObtenerDataEnRecurso($sql);
        
        while ($fila = mysql_fetch_array($rs)){
            $comentarioImagenArr[$fila["pl_id"]]= array("id"=>$fila["pl_id"],
                                                        "fecha"=>$fila["pl_date"],
                                                        "uid"=>$fila["user_id"],
                                                        "usuario"=>$fila["user_name"]);
        }
        
        $bd->DesconectarBD();
        
        return $comentarioImagenArr;
}

function cargar_friendList($id){
        
    include_once 'bd_conn_class/conexionMysql.php';
    
    $bd = new conexionMysql();
    $bd->ConectarBD();
    
    
    $sql  =  " SELECT user_id, user_uf_id, user_profile_pic,  "
            ." concat(user_fname, ' ', user_lname) as user_name, "
            ." user_sys_name "
            ." FROM user "
            ." WHERE user_id IN ( SELECT f_u_id_o " 
            ."                    FROM friend "
            ."                    WHERE f_u_id_d = '".$id."' " 
            ."                        AND f_status = 1) "
            ." AND user_status = 1 ";
    
    $sql .=   " UNION ";
    
    $sql .=  " SELECT user_id, user_uf_id, user_profile_pic,  "
            ." concat(user_fname, ' ', user_lname) as user_name, "
            ." user_sys_name "
            ." FROM user "
            ." WHERE user_id IN ( SELECT f_u_id_d " 
            ."                    FROM friend "
            ."                    WHERE f_u_id_o = '".$id."' " 
            ."                        AND f_status = 1) "
            ." AND user_status = 1 ";
    
    
    $rs = $bd->ObtenerDataEnRecurso($sql);
        
    while ($fila = mysql_fetch_array($rs)){
        $friendsArr[$fila["user_id"]]= array("id"=>$fila["user_id"],
                                            "u_fb_id"=>$fila["user_uf_id"],
                                            "upic"=>$fila["user_profile_pic"],
                                            "usuario"=>$fila["user_name"],
                                            "user_sys_name"=>$fila["user_sys_name"]);
    }
    
    $bd->DesconectarBD();
    
    return $friendsArr;
}


function cargar_friendList_event($eid){
        
    include_once 'bd_conn_class/conexionMysql.php';
    
    $bd = new conexionMysql();
    $bd->ConectarBD();
    
    
    $sql  =  " SELECT user_id, user_uf_id, user_profile_pic,  "
            ." concat(user_fname, ' ', user_lname) as user_name, "
            ." user_sys_name "
            ." FROM user "
            ." WHERE user_id IN ( SELECT ef_u_id " 
            ."                    FROM event_friend "
            ."                    WHERE ef_e_id = '".$eid."' " 
            ."                        AND ef_status = 1) "
            ." AND user_status = 1 ";

    
    
    $rs = $bd->ObtenerDataEnRecurso($sql);
        
    while ($fila = mysql_fetch_array($rs)){
        $friendsArr[$fila["user_id"]]= array("id"=>$fila["user_id"],
                                            "u_fb_id"=>$fila["user_uf_id"],
                                            "upic"=>$fila["user_profile_pic"],
                                            "usuario"=>$fila["user_name"],
                                            "user_sys_name"=>$fila["user_sys_name"]);
    }
    
    $bd->DesconectarBD();
    
    return $friendsArr;
}

function cargar_friend_inv_fb_list(){
    
    include_once 'bd_conn_class/conexionMysql.php';
    
    $bd = new conexionMysql();
    $bd->ConectarBD();
    
    
    $sql =   " SELECT user_id, user_uf_id, user_profile_pic,  "
            ." concat(user_fname, ' ', user_lname) as user_name "
            ." FROM user "
            ." WHERE user_id IN ( SELECT fif_u_id "
            ."                     FROM friend_inv_face "
            ."                     WHERE fif_fb_id = ( SELECT user_uf_id "
            ."                                         FROM user "
            ."                                         WHERE user_id = '".$_SESSION["g2uId"]."' "
            ."                                             AND user_status = 1) "
            ."                         AND fif_status = 1) "
            ."     AND user_status = 1 ";
    
    
    $rs = $bd->ObtenerDataEnRecurso($sql);
        
    while ($fila = mysql_fetch_array($rs)){
        $friendFBInvitatioArr[$fila["user_id"]]= array("id"=>$fila["user_id"],
                                                    "u_fb_id"=>$fila["user_uf_id"],
                                                    "upic"=>$fila["user_profile_pic"],
                                                    "usuario"=>$fila["user_name"]);
    }
    
    $bd->DesconectarBD();
    
    return $friendFBInvitatioArr;
}

function cargar_friend_inv_fb_sent_list(){
    
    include_once 'bd_conn_class/conexionMysql.php';
    
    $bd = new conexionMysql();
    $bd->ConectarBD();
    
    
    $sql =   " SELECT user_id, user_uf_id, user_profile_pic,  "
            ." concat(user_fname, ' ', user_lname) as user_name "
            ." FROM user "
            ." WHERE user_id IN ( SELECT fif_u_id "
            ."                     FROM friend_inv_face "
            ."                     WHERE fif_fb_id = ( SELECT user_uf_id "
            ."                                         FROM user "
            ."                                         WHERE user_id = '".$_SESSION["g2uId"]."' "
            ."                                             AND user_status = 1) "
            ."                         AND fif_status = 1) "
            ."     AND user_status = 1 ";
    
    
    $rs = $bd->ObtenerDataEnRecurso($sql);
        
    while ($fila = mysql_fetch_array($rs)){
        $friendFBInvitatioArr[$fila["user_id"]]= array("id"=>$fila["user_id"],
                                                    "u_fb_id"=>$fila["user_uf_id"],
                                                    "upic"=>$fila["user_profile_pic"],
                                                    "usuario"=>$fila["user_name"]);
    }
    
    $bd->DesconectarBD();
    
    return $friendFBInvitatioArr;
}

function registrar_invitacion_usuario_facebook($req_id, $arrUsers){
        include_once 'bd_conn_class/conexionMysql.php';
        
        $bd = new conexionMysql();
        $bd->ConectarBD();
        
        foreach ($arrUsers as $user) {
                $sql = "INSERT INTO `friend_inv_face` "
                    ." (`fif_id`, `fif_u_id`, `fif_fb_id`, " 
                    ." `fif_fb_req`, `fif_date`, `fif_status`) "
                    ." VALUES "
                    ." (NULL, '".$_SESSION["g2uId"]."', '".$user."', "
                    ." '".$req_id."', NOW(), '1');";

                $bd->EjecutarQuery($sql);
        }
        
        $bd->DesconectarBD();
}

function atualizar_invitacion_usuario_facebook($user_id){
        include_once 'bd_conn_class/conexionMysql.php';
        
        $bd = new conexionMysql();
        $bd->ConectarBD();
        
        $sql = "UPDATE `friend_inv_face` "
            ."  SET `fif_status` = 2  "
            ."  WHERE "
            ."      fif_u_id = $user_id "
            ."  AND fif_fb_id = ".$_SESSION["g2uFacebookUserId"]." ";

        $bd->EjecutarQuery($sql);
        
        $bd->DesconectarBD();
}

function registrar_friendship($uOrigen, $uDestino){
    
    include_once 'bd_conn_class/conexionMysql.php';
        
    $bd = new conexionMysql();
    $bd->ConectarBD();
    
    $sql =   " INSERT INTO `friend` "
            ." (`f_id`, `f_u_id_o`, `f_u_id_d`, `f_status`) "
            ." VALUES "
            ." (NULL, '".$uOrigen."', '".$uDestino."', '1')";
    
    $bd->EjecutarQuery($sql);
        
    $bd->DesconectarBD();
}

function validar_usuario_existe($user_sys_name){
        include_once 'bd_conn_class/conexionMysql.php';
        $DB = new conexionMysql();
        $DB->ConectarBD();
        
        $query = " SELECT user_id FROM user WHERE user_sys_name = '".$user_sys_name."' " ;
        
        $rs = $DB->ObtenerDataEnRecurso($query);        
        
        while ($fila = mysql_fetch_array($rs)){
            $result = $fila["user_id"];
        }
        
        $DB->DesconectarBD();        
        return $result;     
}

function validar_friendship($usuario_session , $id){
    include_once 'bd_conn_class/conexionMysql.php';
    $DB = new conexionMysql();
    $DB->ConectarBD();


    $query = " SELECT 1 as cont "
            ." FROM friend "
            ." WHERE ((f_u_id_d = $id "
            ."		AND f_u_id_o = $usuario_session) "
            ."	OR "
            ."	  (f_u_id_o =  $id "
            ."		AND f_u_id_d = $usuario_session)) "
            ."	AND f_status = 1 ";
//cho $query;
    $rs = $DB->ObtenerDataEnRecurso($query);        

    while ($fila = mysql_fetch_array($rs)){
        $result = $fila["cont"];
    }

    $DB->DesconectarBD();        
    return $result;      
}

function eliminar_foto_wishlist($id){
        include_once 'bd_conn_class/conexionMysql.php';
        
        $bd = new conexionMysql();
        $bd->ConectarBD();
        
        $sql = "UPDATE `photo` "
            ."  SET `ph_status` = 0  "
            ."  WHERE "
            ."  ph_u_id = ".$_SESSION["g2uId"]." "
            ."  AND ph_id =  "
            ."     (SELECT p_ph_id  "
            ."     FROM post  "
            ."     WHERE p_id = $id ) ";

        $bd->EjecutarQuery($sql);
        
        $sql = "UPDATE `wishlist` "
            ."  SET `wl_status` = 0  "
            ."  WHERE "
            ."  wl_u_id = ".$_SESSION["g2uId"]." "
            ."  AND w_id =  "
            ."     (SELECT p_wl_id  "
            ."     FROM post  "
            ."     WHERE p_id = $id ) ";

        $bd->EjecutarQuery($sql);
        
                $sql = "UPDATE `post` "
            ."  SET `p_status` = 0  "
            ."  WHERE "
            ."  p_u_id = ".$_SESSION["g2uId"]." "
            ."  AND  p_id = $id  ";

        $bd->EjecutarQuery($sql);
        
        
        $bd->DesconectarBD();
}

function cargarSesionAmigo($idUsuarioAmigo){
    include_once 'bd_conn_class/conexionMysql.php';
    include_once 'recursos/fechas.php';
    $date = new fechas();
    
    $con = new conexionMysql();
    $con->ConectarBD();
    
    $sql = " SELECT *  "
          ." FROM user "
          ." WHERE user_id = '".$idUsuarioAmigo."' ";
    
    $rs = $con->ObtenerDataEnRecurso($sql);
    
    while ($fila = mysql_fetch_array($rs)){
        
        $_SESSION["g2uFriendId"] = $fila["user_id"];
        
        $_SESSION["g2uFriendSystemName"] = $fila["user_sys_name"];
        
        $_SESSION["g2uFriendName"] = $fila["user_name"];
        $_SESSION["g2uFriendMail"] = $fila["user_mail"];
        $_SESSION["g2uFriendBirthday"] = $date->convertirFechatoNormal($fila["user_birthday"]);

        $_SESSION["g2uFriendFirstName"] = $fila["user_fname"];
        $_SESSION["g2uFriendLastName"] = $fila["user_lname"];
 
        $_SESSION["g2uFriendProfilePic"] = $fila["user_profile_pic"];
        
        $_SESSION["g2uFriendRegDate"] = $fila["user_register_date"];
    }
    
    $con->DesconectarBD();
}

function validarDatosPerfil(){
    
}


function cargar_eventList($user){
    include_once 'bd_conn_class/conexionMysql.php';
    include_once 'recursos/fechas.php';
    $date = new fechas();
    
    $con = new conexionMysql();
    $con->ConectarBD();
    
    
    $sql = " SELECT e_id, e_name, e_f_date, e_u_id, count(ef_u_id) as c  "
          ." FROM event, event_friend "
          ." WHERE e_id = ef_e_id "
          ."     AND (ef_u_id = $user " 
          ."          OR e_u_id = $user ) "
          ." GROUP BY e_id, e_name, e_f_date ";
    
    $rs = $con->ObtenerDataEnRecurso($sql);
    
    while ($fila = mysql_fetch_array($rs)){
        $temp[$fila["e_id"]]["name"] = $fila["e_name"];
        $temp[$fila["e_id"]]["date"] = $date->convertirFechatoNormal($fila["e_f_date"]);
        $temp[$fila["e_id"]]["count"] = $fila["c"];
        $temp[$fila["e_id"]]["creator"] = $fila["e_u_id"];
    }
    $con->DesconectarBD();
    
    return $temp;
}

function guardar_evento($name, $desc, $user, $date){
    include_once 'bd_conn_class/conexionMysql.php';
        
    $bd = new conexionMysql();
    $bd->ConectarBD();
    
    $sql =   " INSERT INTO  `event` ( "
            ." `e_id` , `e_name` , `e_desc` , "
            ." `e_u_id` , `e_c_date` , `e_f_date` "
            ." ) "
            ." VALUES ( "
            ." NULL ,  '".$name."',  '".$desc."', "
            ." '".$user."',  NOW(),  '".$date."' "
            ." ) ";
    
    $bd->EjecutarQuery($sql);
        
    $bd->DesconectarBD();
}

function obtener_max_id_event($user){
    include_once 'bd_conn_class/conexionMysql.php';
        
    $bd = new conexionMysql();
    $bd->ConectarBD();
    
    $sql =   " SELECT MAX(e_id) as id FROM event  "
            ." WHERE e_status = 1 "
            ." AND e_u_id = $user  ";
    
    $rs = $bd->ObtenerDataEnRecurso($sql);
    
    while ($fila = mysql_fetch_array($rs)){
        $resultado = $fila["id"];
    }
        
    $bd->DesconectarBD();
    
    return $resultado;
}

function guardar_evento_amigos($id_event, $usuarios){
    include_once 'bd_conn_class/conexionMysql.php';
        
    $bd = new conexionMysql();
    $bd->ConectarBD();
    
    foreach ($usuarios as $u) {
        if ($u>0){ 
            $sql = " INSERT INTO event_friend "
                  ." (`ef_id`, `ef_e_id`, `ef_u_id`) "
                  ." VALUES "
                  ." (NULL, '".$id_event."', '".$u."')";
            $bd->EjecutarQuery($sql);
        }
    }

    
    $bd->DesconectarBD();
}

function cargar_newsList($id, $friend_list){
    include_once 'bd_conn_class/conexionMysql.php';
        
    $bd = new conexionMysql();
    $bd->ConectarBD();
    $where_in = "";

    if(is_array($friend_list)){
        foreach ($friend_list as $key => $value) {
            $where_in .= $value["id"].",";
        }
    }
    $where_in .= $_SESSION["g2uId"];
    
    $sql =   " SELECT * FROM post, user  "
            ." WHERE p_status = 1 "
            ."     AND p_u_id = user_id  "
            ."     AND p_e_id = 0  "
            ."     AND p_u_id IN ( $where_in )  "
            ." ORDER BY p_date desc "
            ." LIMIT 0, 20 ";
    
    $rs = $bd->ObtenerDataEnRecurso($sql);
    
    while ($fila = mysql_fetch_array($rs)){
        $temp[$fila["p_id"]]= array("id"=>$fila["user_id"],
                                    "upic"=>$fila["user_profile_pic"],
                                    "usuario"=>$fila["user_name"],
                                    "user_sys_name"=>$fila["user_sys_name"],
                                    "desc"=>$fila["p_desc"],
                                    "l_count"=>$fila["p_l_count"],
                                    "c_count"=>$fila["p_c_count"]);
    }
        
    $bd->DesconectarBD();
    
    return $temp;
}

function cargar_news_wishlist($id, $friend_list){
    
        include_once 'bd_conn_class/conexionMysql.php';
        
        $bd = new conexionMysql();
        $where_in = "";

        if(is_array($friend_list)){
            foreach ($friend_list as $key => $value) {
                $where_in .= $value["id"].",";
            }
        }
        
        $where_in .= $_SESSION["g2uId"];

        $sql = " SELECT "
                ." concat(ph_img_mini_route,'/',ph_img_name) as imagen, "
                ." concat(ph_img_route,'/',ph_img_name) as imagen_full, "
                ." ph_img_name as name, "
                ." ph_desc as descripcion, "
                ." p_id "
              ." FROM wishlist, post, photo "
              ." WHERE wl_u_id IN ($where_in) "
                ." AND wl_u_id = p_u_id "
                ." AND wl_u_id = ph_u_id "
                ." AND wl_status = '1' "
                ." AND p_status = '1' "
                ." AND ph_status = '1' "
                ." AND p_wl_id = wl_id "
                ." AND p_ph_id = ph_id "
                ." AND ph_img_mini_route <> '' ";

        $bd->ConectarBD();
        $rs = $bd->ObtenerDataEnRecurso($sql);
        
        while ($fila = mysql_fetch_array($rs)){
            $imagenArr[$fila["p_id"]]= array("img"=>$fila["imagen"],
                                "desc"=>$fila["descripcion"],
                                "imgFull"=>$fila["imagen_full"],
                                "imgName"=>$fila["name"],
                                "idPost"=>$fila["p_id"],
                                "likes"=>$fila["p_l_count"],
                                "comments"=>$fila["p_c_count"],
                                "shares"=>$fila["p_s_count"]);
        }
        
        $bd->DesconectarBD();
        
        return $imagenArr;    
    
}


function cargar_newsList_event($id, $friend_list, $eid){
    include_once 'bd_conn_class/conexionMysql.php';
        
    $bd = new conexionMysql();
    $bd->ConectarBD();
    $where_in = "";

    foreach ($friend_list as $key => $value) {
        $where_in .= $value["id"].",";
    }
    $where_in .= $_SESSION["g2uId"];
    
    $sql =   " SELECT * FROM post, user  "
            ." WHERE p_status = 1 "
            ."     AND p_u_id = user_id  "
            ."     AND p_e_id = $eid  "
            ."     AND p_u_id IN ( $where_in )  "
            ." ORDER BY p_date desc "
            ." LIMIT 0, 20 ";
    
    $rs = $bd->ObtenerDataEnRecurso($sql);
    
    while ($fila = mysql_fetch_array($rs)){
        $temp[$fila["p_id"]]= array("id"=>$fila["user_id"],
                                    "upic"=>$fila["user_profile_pic"],
                                    "usuario"=>$fila["user_name"],
                                    "user_sys_name"=>$fila["user_sys_name"],
                                    "desc"=>$fila["p_desc"],
                                    "l_count"=>$fila["p_l_count"],
                                    "c_count"=>$fila["p_c_count"]);
    }
        
    $bd->DesconectarBD();
    
    return $temp;
}


function validarMail($email) {
       global $HTTP_HOST;
       error_reporting(0);
       $resultado = array();
       if (!eregi("^[_\.0-9a-z\-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,6}$",$email)) {
         $resultadoado[0]=false;
         $resultado['code']="702";
          return $resultado;
       }
       list ( $Username, $dominio ) = split ("@",$email);
         if (getmxrr($dominio, $MXHost))  $conecta_dominio = $MXHost[0];
                  else  $conecta_dominio = $dominio;

        $errno="";
        $errstr="";
        $conectar = fsockopen ( $conecta_dominio, 25, $errno, $errstr, 5 );

      if ($conectar) {

        if (ereg("^220", $ver = fgets($conectar, 1024))) {

           fputs ($conectar, "HELO $HTTP_HOST\r\n");
           $ver = fgets ( $conectar, 1024 );
           fputs ($conectar, "MAIL FROM: <{$email}>\r\n");
           $From = fgets ( $conectar, 1024 );
           fputs ($conectar, "RCPT TO: <{$email}>\r\n");
           $To = fgets ($conectar, 1024);
           fputs ($conectar, "QUIT\r\n");
           fclose($conectar);
           if (!ereg ("^250", $From) || !ereg ( "^250", $To )) {
               $resultado[0]=false;
               $resultado['code']="700";
               return $resultado;
            }
        }
           else {
              $resultado[0] = false;
              $resultado['code'] = "Død";
              return $resultado;
            }
      }
        else {
            $resultado[0]=false;
            $resultado['code']="701";
            return $resultado;
      }

       $resultado[0]=true;
       $resultado['code']="200";
       return $resultado;
}



?>
